package com.yjy.mybase.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;


import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.yjy.mybase.utils.AppManager;
import com.yjy.mybase.utils.CrashHandler;
import com.yjy.mybase.utils.ToastUtils;


import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * author : YangJinyang
 * create : 2019-10-28 15:53
 * description :
 */
public abstract class BaseActivity extends AppCompatActivity {

    Unbinder mUnbinder;

    @Autowired()
    Bundle bundle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**
         * 路由绑定对象
         */
        ARouter.getInstance().inject(this);

        Log.e("onCreate", "savedInstanceState:" + savedInstanceState);
        if (savedInstanceState != null) {
//            startActivity();
//            Intent intent = new Intent(getApplicationContext(), SplashActivity.class);
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            getApplication().startActivity(intent);
//            android.os.Process.killProcess(android.os.Process.myPid());
        }

        //强制竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

        //取消ActionBar
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

        //设置界面随键盘弹出自动上移
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        setContentView(layout());


        //绑定ButterKnife
        mUnbinder = ButterKnife.bind(this);
        //添加Activity到管理堆栈
        AppManager.getInstance().addActivity(this);
        // 异常处理，不需要处理时注释掉这两句即可！

        CrashHandler crashHandler = CrashHandler.getInstance();

// 注册crashHandler
        crashHandler.init(this);
        //初始化
        onActivityCreated();
    }


    /**
     * 获取传过来的值，页面传值全部都用bundle
     *
     * @return
     */
    protected Bundle getBundle() {
        return bundle;
    }


    /**
     * 设置布局
     *
     * @return 布局资源id
     */
    public abstract
    @LayoutRes
    int layout();

    /**
     * 初始化
     */
    private void onActivityCreated() {
        initView();
        initData();

    }


    public abstract void initView();

    public abstract void initData();


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解除ButterKnife的绑定
        mUnbinder.unbind();
        //从管理堆栈移除Activity
        AppManager.getInstance().finishActivity(this);
    }

    /**
     * 隐藏输入法
     *
     * @param view
     */
    public void hideKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        if (imm != null)
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /**
     * 显示输入法
     *
     * @param view
     */
    public void showKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null)
            imm.showSoftInput(view, 0);
    }

    /**
     * 获得StatusBar的高度
     *
     * @return StatusBar的高度
     */
    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    /**
     * 设置StatusBar透明
     */
    public void setTranslucentStatus(boolean darkFont) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0 全透明实现
            //getWindow.setStatusBarColor(Color.TRANSPARENT)
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            if (darkFont)
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            else
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //4.4 全透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    /**
     * 跳转页面
     *
     * @param arouter_url
     * @param data
     */
    public void startActivity(String arouter_url, Bundle data) {
        if (!TextUtils.isEmpty(arouter_url)) {
            if (null != data && data.isEmpty()) {
                ARouter.getInstance().build(arouter_url)
                        .with(data)
                        .navigation();
            } else {
                ARouter.getInstance().build(arouter_url).navigation();
            }
        }

    }


    public void startService(Class clz) {
        Intent intent = new Intent(this, clz);
        startService(intent);
    }

    public void startService(Class clz, Bundle bundle) {
        Intent intent = new Intent(this, clz);
        if (bundle != null)
            intent.putExtras(bundle);
        startService(intent);
    }

    public View getDecorView() {
        return getWindow().getDecorView();
    }

    /**
     * 创建状态栏并且设置状态栏颜色
     *
     * @param color 颜色
     */
    public void setStatusBarColor(int color, boolean darkFont) {
        setTranslucentStatus(darkFont);
        ViewGroup contentLayout = findViewById(android.R.id.content);
        setupStatusBarView(this, contentLayout, color);
        View contentView = contentLayout.getChildAt(1);
        //如果是DrawerLayout,让内部第一个布局设置padding
        if (contentView instanceof DrawerLayout)
            contentView = ((DrawerLayout) contentView).getChildAt(0);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
        params.setMargins(0, getStatusBarHeight(), 0, 0);
    }

    /**
     * 更改状态栏颜色
     */
    public void changeStatusBarColor(int color, boolean darkFont) {
        setTranslucentStatus(darkFont);
        ViewGroup contentLayout = findViewById(android.R.id.content);
        View statusBarView = contentLayout.getChildAt(0);
        statusBarView.setBackgroundColor(color);
    }

    /**
     * 创建view替换StatusBar
     *
     * @param activity
     * @param contentLayout
     * @param color
     */
    private void setupStatusBarView(Activity activity, ViewGroup contentLayout, int color) {
        View statusBarView = new View(activity);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight());
        contentLayout.addView(statusBarView, 0, lp);
        statusBarView.setBackgroundColor(color);
    }

    /**
     * 隐藏StatusBarView
     */
    public void hideStatusBarView() {
        ViewGroup contentLayout = findViewById(android.R.id.content);

        contentLayout.getChildAt(0).setVisibility(View.GONE);

        View contentView = contentLayout.getChildAt(1);
        //如果是DrawerLayout,让内部第一个布局设置padding
        if (contentView instanceof DrawerLayout)
            contentView = ((DrawerLayout) contentView).getChildAt(0);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
        params.setMargins(0, 0, 0, 0);
    }

    /**
     * Toast
     *
     * @param message 消息
     */
    public void toast(String message) {
        ToastUtils.show(message);
    }


}
